package com.ljfiction.book.fragment.pepper;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.ljfiction.book.Baner.PepperhavestBean;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.UserCenterActivity;
import com.ljfiction.book.fragment.BaseFragment;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.ScreenUtils;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.TimeTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Android on 2018/2/8.
 */

public class JinjiaoFragment extends BaseFragment {

    private RecyclerView myshupingrecycler;
    private SmartRefreshLayout refreshMyshupingrecycle;
    private LinearLayout zanwushuju;
    private BaseQuickAdapter mAdapter;
    private int page = 1;
    private List<XmjBean.DataBean.ListBean> datas = new ArrayList<>();
    private XmjBean xmjBean;
    private Context context;
    private Intent intent;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myshuping_recycaler, container, false);
        myshupingrecycler=view.findViewById(R.id.myshupingrecycler);
        refreshMyshupingrecycle=view.findViewById(R.id.refresh_myshupingrecycle);
        zanwushuju=view.findViewById(R.id.zanwushuju);
        context=getContext();
        intView();
        intData();
        return view;
    }
    private void intView() {
        myshupingrecycler.setAdapter(mAdapter = new BaseQuickAdapter<XmjBean.DataBean.ListBean, BaseViewHolder>(R.layout.xmjandjj_item, datas) {
            @Override
            protected void convert(BaseViewHolder helper, XmjBean.DataBean.ListBean item) {
                AutoUtils.autoSize(helper.itemView);
                if ((helper.getLayoutPosition()) % 4 == 0) {
                    helper.setBackgroundRes(R.id.leftview_jinjiao, R.drawable.shape_peperleft);
                    helper.setBackgroundRes(R.id.jinjiaobiankuang, R.drawable.ljbiankuang1);
                } else if ((helper.getLayoutPosition()) % 4 == 1) {
                    helper.setBackgroundRes(R.id.leftview_jinjiao, R.drawable.shape_peperlsft2);
                    helper.setBackgroundRes(R.id.jinjiaobiankuang, R.drawable.ljbiankuang2);
                } else if ((helper.getLayoutPosition()) % 4 == 2) {
                    helper.setBackgroundRes(R.id.leftview_jinjiao, R.drawable.shape_peperlsft3);
                    helper.setBackgroundRes(R.id.jinjiaobiankuang, R.drawable.ljbiankuang3);
                } else if ((helper.getLayoutPosition()) % 4 == 3) {
                    helper.setBackgroundRes(R.id.leftview_jinjiao, R.drawable.shape_peperlsft4);
                    helper.setBackgroundRes(R.id.jinjiaobiankuang, R.drawable.ljbiankuang4);
                }
                helper.setText(R.id.persenname_jinjiao, item.getPseudonym())
                        .setText(R.id.time_jinjiao, TimeTool.getTimeByLong(item.getGiveDateTime()))
                        .setText(R.id.booktitle_jinjiao, "《"+item.getBookName()+"》")
                        .setText(R.id.num_jinjiao, String.valueOf(item.getGoldenTicketCount()));
                Glide.with(mContext).load(item.getUserHeadPortraitURL()).into((ImageView) helper.getView(R.id.personimage_jinjiao));
                helper.addOnClickListener(R.id.personimage_jinjiao);
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                intent = new Intent(context, UserCenterActivity.class);
                intent.putExtra("userId", String.valueOf(datas.get(position).getUserId()));
                startActivity(intent);
            }
        });
        myshupingrecycler.setLayoutManager(new LinearLayoutManager(context));
        refreshMyshupingrecycle.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if (xmjBean.getData().isIsLastPage()){
                    refreshlayout.finishLoadMore();
                    refreshMyshupingrecycle.setEnableLoadMore(false);
                }else {
                    page++;
                    intData();

                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                datas.clear();
                refreshMyshupingrecycle.setEnableLoadMore(true);
                intData();
            }
        });
    }
    private void intData() {
        OkHttpUtils.post(HttpManager.IP+"/getAuthorGoldenticketlog")

                .params("startpage",String.valueOf(page))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        refreshMyshupingrecycle.finishRefresh();
                        refreshMyshupingrecycle.finishLoadMore();
                        Gson gs=new Gson();
                        try {
                            xmjBean = gs.fromJson(s,XmjBean.class);
                            if (xmjBean.getReturnCode()==200){
                                if (zanwushuju.getVisibility()==View.VISIBLE){
                                    zanwushuju.setVisibility(View.GONE);
                                }
                                datas.addAll(xmjBean.getData().getList());
                                mAdapter.notifyDataSetChanged();
                            }else {
                                if (datas.size()<1){
                                    zanwushuju.setVisibility(View.VISIBLE);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}
